<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button class="set">创建</button>
    <button class="get">获取</button>
</body>
<script>
    var set = document.getElementsByClassName("set")[0];
    var get = document.getElementsByClassName("get")[0];

    // 如何获取cookie?
    // cookie 的存取都依赖于 document.cookie   (类型  字符串类型)
    //  cookie 存储的时候是一条一条的的存的,取的时候 一次性获取所有数据 用"; "分隔

    set.onclick = function () {
        var date = new Date();
        date.setDate(date.getDate() + 7);

        document.cookie = "user=a123123; expires=" + date.toUTCString() + ";path=/";
        document.cookie = "pwd=123123; expires=" + date.toUTCString() + ";path=/";
        document.cookie = "phone=17386141517; expires=" + date.toUTCString() + ";path=/";
        document.cookie = "email=123123@qq.com; expires=" + date.toUTCString() + ";path=/";
    }

    get.onclick = function(){
        console.log(document.cookie);

        var data = {};
        var cookie = document.cookie;
        if(cookie){
            var list = cookie.split("; ");
            console.log(list);
            for(var i=0;i<list.length;i++){
                var item = list[i];  // 'user=a123123', 'pwd=123123'
                var key = item.split("=")[0];
                var val = item.split("=")[1];
                data[key] = val;
            }
        
        }
        console.log(data);
    }


</script>

</html>